package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.arcus.ArcusUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SegUploadDetailMapper;
import com.ruoyi.system.domain.SegUploadDetail;
import com.ruoyi.system.service.ISegUploadDetailService;

/**
 * (测试)上传表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-26
 */
@Service
public class SegUploadDetailServiceImpl implements ISegUploadDetailService
{
    @Autowired
    private SegUploadDetailMapper segUploadDetailMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<String> selectUploadTokenByUserId(Long userId) {
        return segUploadDetailMapper.selectUploadTokenByUserId(userId);
    }

    @Override
    public ArcusUser userInfoConvert(String userName, boolean n2i) {
        SysUser user = sysUserMapper.selectUserByUserName(userName);
        ArcusUser u = new ArcusUser();
        if (n2i){  // name 2 id
            u.user_id = user.getUserId();
        }else{
            u.user_name = user.getUserName();
        }
        return u;
    }

    @Override
    public String uploadInfoInsertMulti(Long userId, String uploadToken, ArrayList<String> imgList) {
        // 获得上传已经上传的列表（前台完全改完后，这段代码删除)
        SegUploadDetail tmp = new SegUploadDetail();
        tmp.setId(userId); tmp.setUploadToken(uploadToken);
        List<SegUploadDetail> hasUploadList = selectSegUploadDetailList(tmp);
        Map<String, Integer> tmpMap = new HashMap<>();
        for(SegUploadDetail d: hasUploadList){
            tmpMap.put(d.getImgName(), 0);
        }
        // 以上是等待删除的代码

        for(String imgName: imgList){
            if(tmpMap.get(imgName) == null){
                // 这里才是正常的代码
                SegUploadDetail t = new SegUploadDetail();
                t.setUserId(userId);
                t.setUploadToken(uploadToken);
                t.setImgName(imgName);
                try{
                    segUploadDetailMapper.insertSegUploadDetail(t);
                }catch (Exception e){
                    e.printStackTrace();
                    return e.getMessage();
                }
                // 正常代码结束
            }
        }

        return "ok";
    }

    /**
     * 查询(测试)上传表
     *
     * @param id (测试)上传表主键
     * @return (测试)上传表
     */
    @Override
    public SegUploadDetail selectSegUploadDetailById(Long id)
    {
        return segUploadDetailMapper.selectSegUploadDetailById(id);
    }

    /**
     * 查询(测试)上传表列表
     *
     * @param segUploadDetail (测试)上传表
     * @return (测试)上传表
     */
    @Override
    public List<SegUploadDetail> selectSegUploadDetailList(SegUploadDetail segUploadDetail)
    {
        return segUploadDetailMapper.selectSegUploadDetailList(segUploadDetail);
    }

    /**
     * 新增(测试)上传表
     *
     * @param segUploadDetail (测试)上传表
     * @return 结果
     */
    @Override
    public int insertSegUploadDetail(SegUploadDetail segUploadDetail)
    {
        return segUploadDetailMapper.insertSegUploadDetail(segUploadDetail);
    }

    /**
     * 修改(测试)上传表
     *
     * @param segUploadDetail (测试)上传表
     * @return 结果
     */
    @Override
    public int updateSegUploadDetail(SegUploadDetail segUploadDetail)
    {
        return segUploadDetailMapper.updateSegUploadDetail(segUploadDetail);
    }

    /**
     * 批量删除(测试)上传表
     *
     * @param ids 需要删除的(测试)上传表主键
     * @return 结果
     */
    @Override
    public int deleteSegUploadDetailByIds(Long[] ids)
    {
        return segUploadDetailMapper.deleteSegUploadDetailByIds(ids);
    }

    /**
     * 删除(测试)上传表信息
     *
     * @param id (测试)上传表主键
     * @return 结果
     */
    @Override
    public int deleteSegUploadDetailById(Long id)
    {
        return segUploadDetailMapper.deleteSegUploadDetailById(id);
    }
}
